﻿/*
* ========================================================================
* 类说明：
*   
* 
* 作者：HGS   时间：2024/12/3 10:10:21
* 
* 修改者：         时间：           
* 修改说明：
* ========================================================================
*/

using EasyTest.Shared.EnumType;
using EasyTest.Shared.TestAttribute;
using EasyTest.Shared.外部访问;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZLGCANoptimize.方法;

namespace ZLGCANoptimize
{
    public class USB新
    {
        [ExposedMethodAttribute("")]
        public bool 切换USB音源(int 通道, int USB通道, out string 信息, out string 响应值, out string 命令)
        {
            信息 = "";
            响应值 = "";
            命令 = "";
            try
            {
                string cmd = "";
#if ELO20240927
                switch (USB通道)
                {
                    case 2: cmd =  "04 2E 4C 10 03 00 00 00"; break;
                    default: cmd = "04 2E 4C 10 03 00 00 00"; break;
                }
                命令 = cmd;
                List<CanCmdModel> cmdList = new List<CanCmdModel>();
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = cmd });
#else
                switch (USB通道)
                {
                    case 2:  cmd = "10 2E 4C 10 03 00 00 00"; break;
                    default: cmd = "10 2E 4C 10 03 00 00 00"; break;
                }
                命令 = cmd;
                List<CanCmdModel> cmdList = new List<CanCmdModel>();
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = cmd });
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = "21 00 00 00 00 00 00 00" });
#endif
                CanReadRequestModel requestModel = new CanReadRequestModel();
                requestModel.Channel = (ushort)通道;
                requestModel.IsClearCache = false;
                requestModel.IsDecode = true;
                requestModel.DecodeLevel = "62";
                requestModel.CmdList = cmdList;
                requestModel.SendCmdTimeSpan = 50;
                requestModel.ReceiveCmdTimeSpan = 1500;
                requestModel.MaxSendCount = 1;
                requestModel.AutoParse = true;
                requestModel.IsVerifyDid = true;
                requestModel.RequestMode = CanRequestMode.发送并接收;

                var 发送响应 = 数据.发送指令(requestModel);
                if (发送响应.Success == false)
                {
                    信息 = 发送响应.ErrorMessage;
                    响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                    return false;
                }

                响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 10", out string 目标值);
                if (!提取结果)
                {
                    信息 = "切换USB音源失败";
                    return false;  //指定数据帧不存在
                }

                信息 = "切换USB音源成功";
                return true;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                响应值 = "";
                //LogHelper.Error($"[USB.切换USB音源]异常，{ex.Message}，{ex.StackTrace}");
                eLog.AddLog($"[USB.切换USB音源]异常，{ex.Message}，{ex.StackTrace}", eMsgType.Error);
            }
            return false;
        }

        [ExposedMethodAttribute("")]
        public bool 指定USB曲目(int 通道, int 曲目, out string 信息, out string 响应值)
        {
            信息 = "";
            响应值 = "";
            try
            {
                List<CanCmdModel> cmdList = new List<CanCmdModel>();
#if ELO20240927
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00" });
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = $"21 11 28 00 00 00 00 00" });
#else
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00" });
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = $"21 00 28 00 00 00 00 00" });
#endif
                CanReadRequestModel requestModel = new CanReadRequestModel();
                requestModel.Channel = (ushort)通道;
                requestModel.IsClearCache = false;
                requestModel.IsDecode = true;
                requestModel.DecodeLevel = "62";
                requestModel.CmdList = cmdList;
                requestModel.SendCmdTimeSpan = 50;
                requestModel.ReceiveCmdTimeSpan = 1500;
                requestModel.MaxSendCount = 1;
                requestModel.AutoParse = true;
                requestModel.IsVerifyDid = true;
                requestModel.RequestMode = CanRequestMode.发送并接收;

                var 发送响应 = 数据.发送指令(requestModel);
                if (发送响应.Success == false)
                {
                    信息 = 发送响应.ErrorMessage;
                    响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                    return false;
                }

                响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 26", out string 目标值);
                if (!提取结果)
                {
                    信息 = "设置USB曲目失败";
                    return false;  //指定数据帧不存在
                }

                信息 = "设置USB曲目成功";
                return true;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                响应值 = "";
                //LogHelper.Error($"[USB.指定USB曲目]异常，{ex.Message}，{ex.StackTrace}");
                eLog.AddLog($"[USB.指定USB曲目]异常，{ex.Message}，{ex.StackTrace}", eMsgType.Error);
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 播放指定USB曲目(int 通道, int 曲目, string 播放长度Hex, out string 信息, out string 响应值)
        {
            信息 = "";
            响应值 = "";
            try
            {
                string cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00";
                List<CanCmdModel> cmdList = new List<CanCmdModel>();
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00" });
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = $"21 00 {播放长度Hex} 00 00 00 00 00" });
                CanReadRequestModel requestModel = new CanReadRequestModel();
                requestModel.Channel = (ushort)通道;
                requestModel.IsClearCache = false;
                requestModel.IsDecode = true;
                requestModel.DecodeLevel = "62";
                requestModel.CmdList = cmdList;
                requestModel.SendCmdTimeSpan = 50;
                requestModel.ReceiveCmdTimeSpan = 1500;
                requestModel.MaxSendCount = 1;
                requestModel.AutoParse = true;
                requestModel.IsVerifyDid = true;
                requestModel.RequestMode = CanRequestMode.发送并接收;

                var 发送响应 = 数据.发送指令(requestModel);
                if (发送响应.Success == false)
                {
                    信息 = 发送响应.ErrorMessage;
                    响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                    return false;
                }

                响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 26", out string 目标值);
                if (!提取结果)
                {
                    信息 = "播放指定USB曲目失败";
                    return false;  //指定数据帧不存在
                }

                信息 = "播放指定USB曲目成功";
                return true;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                响应值 = "";
               // LogHelper.Error($"[USB.播放指定USB曲目]异常，{ex.Message}，{ex.StackTrace}");
                eLog.AddLog($"[USB.播放指定USB曲目]异常，{ex.Message}，{ex.StackTrace}", eMsgType.Error);
            }
            return false;
        }


        //=================================== 2024-09-27 ELO指令更新，以下方法为对应更新的内容 ==========================================
        [ExposedMethodAttribute("")]
        public bool 切换USB音源4C10(int 通道, int USB通道, out string 信息, out string 响应值, out string 命令)
        {
            信息 = "";
            响应值 = "";
            命令 = "";
            try
            {
                string cmd = "";
                switch (USB通道)
                {
                    case 2: cmd = "04 2E 4C 10 03 00 00 00"; break;
                    default: cmd = "04 2E 4C 10 03 00 00 00"; break;
                }
                命令 = cmd;

                List<CanCmdModel> cmdList = new List<CanCmdModel>();
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = cmd });
                CanReadRequestModel requestModel = new CanReadRequestModel();
                requestModel.Channel = (ushort)通道;
                requestModel.IsClearCache = false;
                requestModel.IsDecode = true;
                requestModel.DecodeLevel = "62";
                requestModel.CmdList = cmdList;
                requestModel.SendCmdTimeSpan = 50;
                requestModel.ReceiveCmdTimeSpan = 1500;
                requestModel.MaxSendCount = 1;
                requestModel.AutoParse = true;
                requestModel.IsVerifyDid = true;
                requestModel.RequestMode = CanRequestMode.发送并接收;

                var 发送响应 = 数据.发送指令(requestModel);
                if (发送响应.Success == false)
                {
                    信息 = 发送响应.ErrorMessage;
                    响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                    return false;
                }

                响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 10", out string 目标值);
                if (!提取结果)
                {
                    信息 = "切换USB音源失败";
                    return false;  //指定数据帧不存在
                }

                信息 = "切换USB音源成功";
                return true;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                响应值 = "";
                //LogHelper.Error($"[USB.切换USB音源4C10]异常，{ex.Message}，{ex.StackTrace}");
                eLog.AddLog($"[USB.切换USB音源4C10]异常，{ex.Message}，{ex.StackTrace}", eMsgType.Error);
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 指定USB曲目4C26(int 通道, int 曲目, out string 信息, out string 响应值)
        {
            信息 = "";
            响应值 = "";
            try
            {
                List<CanCmdModel> cmdList = new List<CanCmdModel>();
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00" });
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = $"21 11 28 00 00 00 00 00" });
                CanReadRequestModel requestModel = new CanReadRequestModel();
                requestModel.Channel = (ushort)通道;
                requestModel.IsClearCache = false;
                requestModel.IsDecode = true;
                requestModel.DecodeLevel = "62";
                requestModel.CmdList = cmdList;
                requestModel.SendCmdTimeSpan = 50;
                requestModel.ReceiveCmdTimeSpan = 1500;
                requestModel.MaxSendCount = 1;
                requestModel.AutoParse = true;
                requestModel.IsVerifyDid = true;
                requestModel.RequestMode = CanRequestMode.发送并接收;

                var 发送响应 = 数据.发送指令(requestModel);
                if (发送响应.Success == false)
                {
                    信息 = 发送响应.ErrorMessage;
                    响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                    return false;
                }

                响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 26", out string 目标值);
                if (!提取结果)
                {
                    信息 = "设置USB曲目失败";
                    return false;  //指定数据帧不存在
                }

                信息 = "设置USB曲目成功";
                return true;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                响应值 = "";
                //LogHelper.Error($"[USB.指定USB曲目4C26]异常，{ex.Message}，{ex.StackTrace}");
                eLog.AddLog($"[USB.指定USB曲目4C26]异常，{ex.Message}，{ex.StackTrace}", eMsgType.Error);
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 停止播放USB音源4C20(int 通道, int USB通道, out string 信息, out string 响应值, out string 命令)
        {
            信息 = "";
            响应值 = "";
            命令 = "";
            try
            {
                string cmd = "";
                switch (USB通道)
                {
                    case 2:  cmd = "04 2E 4C 20 02 00 00 00"; break;
                    default: cmd = "04 2E 4C 20 02 00 00 00"; break;
                }
                命令 = cmd;

                List<CanCmdModel> cmdList = new List<CanCmdModel>();
                cmdList.Add(new CanCmdModel() { Id = 0x761, cmd = cmd });
                CanReadRequestModel requestModel = new CanReadRequestModel();
                requestModel.Channel = (ushort)通道;
                requestModel.IsClearCache = false;
                requestModel.IsDecode = true;
                requestModel.DecodeLevel = "62";
                requestModel.CmdList = cmdList;
                requestModel.SendCmdTimeSpan = 50;
                requestModel.ReceiveCmdTimeSpan = 1500;
                requestModel.MaxSendCount = 1;
                requestModel.AutoParse = true;
                requestModel.IsVerifyDid = true;
                requestModel.RequestMode = CanRequestMode.发送并接收;

                var 发送响应 = 数据.发送指令(requestModel);
                if (发送响应.Success == false)
                {
                    信息 = 发送响应.ErrorMessage;
                    响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                    return false;
                }

                响应值 = string.Join(",", 发送响应.ResultList ?? new List<string>());
                var 提取结果 = 数据.GetFrameHeaderData(发送响应.ResultList, "03 6E 4C 20", out string 目标值);
                if (!提取结果)
                {
                    信息 = "停止播放USB音源失败";
                    return false;  //指定数据帧不存在
                }

                信息 = "停止播放USB音源成功";
                return true;
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
                响应值 = "";
                //LogHelper.Error($"[USB.停止播放USB音源4C20]异常，{ex.Message}，{ex.StackTrace}");
                eLog.AddLog($"[USB.停止播放USB音源4C20]异常，{ex.Message}，{ex.StackTrace}", eMsgType.Error);
            }
            return false;
        }
    }
}
